//
//  CellNodeController.swift
//  imoney
//
//  Created by zhuxietong on 2020/11/13.
//

import Foundation
import Eelay

public protocol SpecGetterDelegate {
    func specGetter()->_Spec
    func loadCellModel(model:NSMutableDictionary,node:RenderNodeCell)

}

public protocol SpecGetterControllerDelegate {
    func specGetter()->_Spec
    func loadNode(model:NSMutableDictionary,node:RenderNode)

}
open class CellBaseNodeController<Node:JoCellNode>: ASCollectionContainer,SpecGetterDelegate{
    public var renderNode:Node? = nil

    open func loadCellModel(model: NSMutableDictionary, node: RenderNodeCell) {
        if  let _renderNode = node as? Node{
            renderNode = _renderNode
        }        
        
    }
        
    open override var sections:TP.section{
        get{
            return [
                [
                    mo.detail.swift_dictionary
                ]
            ]
        }
    }
    
    open override var selector: TP.selector{
        get{
            return ["<default>":Node.self]
        }
    }
    
    open override func viewDidLoad() {
        super.viewDidLoad()
        nameN__.backgroundColor = .clear
        load(sections: sections, selector: selector)
    }
    
//    open override func mo_detail_reload() {
//        load(sections: sections, selector: selector)
//        tableNode.reloadData()
//    }
    
    let nameN__ = ASTextNode2()
    
    open func specGetter() -> _Spec {
        let s = _Spec.inset([], [20], child: nameN__["20"])
        return s
    }
        
}

open class TableCellBaseNodeController<Node:JoCellNode>: ASTableContainer,SpecGetterDelegate{
    public var renderNode:RenderNodeCell? = nil
    open func loadCellModel(model: NSMutableDictionary, node: RenderNodeCell) {
        renderNode = node
    }
        
    open override var sections:TP.section{
        get{
            return [
                [
                    mo.detail.swift_dictionary
                ]
            ]
        }
    }
    
    open override var selector: TP.selector{
        get{
            return ["<default>":Node.self]
        }
    }
    
    open override func viewDidLoad() {
        super.viewDidLoad()
        nameN__.backgroundColor = .clear
        load(sections: sections, selector: selector)
    }
    
//    open override func mo_detail_reload() {
//        load(sections: sections, selector: selector)
//        tableNode.reloadData()
//    }
    
    let nameN__ = ASTextNode2()
    
    open func specGetter() -> _Spec {
        let s = _Spec.inset([], [20], child: nameN__["20"])
        return s
    }
        
}


open class RenderNodeCell:JoCellNode{
    
    open override func addLayoutRules() {
        backgroundColor = .clear
        
    }
    
    
    
    open override func mo_model_reload() {
        if let ctr = self.delegate as? SpecGetterDelegate{
            return ctr.loadCellModel(model: mo.model, node: self)
        }
    }
    
    open override func specGetter() -> _Spec {
        if let ctr = self.delegate as? SpecGetterDelegate{
            return ctr.specGetter()
        }
        return super.specGetter()
    }
    
    
}



open class CellNodeController: CellBaseNodeController<RenderNodeCell> {
    
}
open class TableCellNodeController: TableCellBaseNodeController<RenderNodeCell> {
    
}

open class RenderNode:MoDisplayNode{
    
    open override func addLayoutRules() {
        backgroundColor = .clear
        
    }
    open override func mo_model_reload() {
        if let ctr = mo.controller as? SpecGetterControllerDelegate{
            ctr.loadNode(model: mo.model, node: self)
        }
        setNeedsLayout()
        layoutIfNeeded()
    }
    
    open override func specGetter() -> _Spec {
        if let ctr = self.mo.controller as? SpecGetterControllerDelegate{
            print("skaksajkdjks---1")
            return ctr.specGetter()
        }
        if let ctr = self.delegate as? SpecGetterControllerDelegate{
            print("skaksajkdjks---2")
            return ctr.specGetter()
        }
        return super.specGetter()
    }
    
    
}


open class BaseNodeController<Node:MoDisplayNode>: TypeInitController,SpecGetterControllerDelegate,EeActionDelegate{
    public func active(onwer: Any, actionID: String, data: NSMutableDictionary) {
        
    }
    
    
    public var contentNode = Node()
    public func loadNode(model: NSMutableDictionary, node: RenderNode) {
        
    }
    
  
    
    open func layContent(node:MoDisplayNode) {
        jo_contentView.eelay = [
            [node.view,[ee.T.L.B.R.safe]]
        ]
    }
    
    open override func viewDidLoad() {
        super.viewDidLoad()
        contentNode.delegate = self
        contentNode.mo.controller = self
        layContent(node: contentNode)
        nameN__.backgroundColor = .clear
        contentNode.mo_model_reload()
    }
    
//    open override func mo_detail_reload() {
//        load(sections: sections, selector: selector)
//        tableNode.reloadData()
//    }
    
    let nameN__ = ASTextNode2()
    
    open func specGetter() -> _Spec {
        let s = _Spec.inset([], [20], child: nameN__["20"])
        return s
    }
        
}
open class NodeController:BaseNodeController<RenderNode>{
    
}

